#include <gio/gio.h>
#include <gio/giotypes.h>

#include <filesystem>
#include <fstream>
#include <iostream>
#include <thread>
#include <vector>

#include <genai/text/chat.h>

void callback(ChatResult *result, void *user_data) {
    fprintf(stdout, "start\n");

    auto getBool = [](bool value) { return value ? "true" : "false"; };
    fprintf(stdout, "%s\n", chat_result_get_assistant_message(result));
    fprintf(stdout, "%s\n", chat_result_get_finish_reason_message(result));
    fprintf(stdout, "%d\n", chat_result_get_error_code(result));
    fprintf(stdout, "%s\n", chat_result_get_error_message(result));
    fprintf(stdout, "%s\n", getBool(chat_result_get_is_end(result)));
    if (user_data != nullptr) {
        int *a = static_cast<int *>(user_data);
        fprintf(stdout, "%d\n", *a);
    }

    fprintf(stdout, "end\n");
}

void chat() {
    GMainLoop *pMainLoop = g_main_loop_new(nullptr, false);

    std::thread ctrlThread([pMainLoop] {
        g_main_loop_run(pMainLoop);
        g_main_loop_unref(pMainLoop);
    });
    ctrlThread.detach();

    ChatModelConfig *config = chat_model_config_create();
    chat_model_config_set_name(config, "百度-ERNIE-Bot-4");
    chat_model_config_set_top_k(config, 0.5);
    chat_model_config_set_deploy_type(config, ModelDeployType::PublicCloud);

    GenAiTextSession *session = genai_text_create_session();
    genai_text_set_model_config(session, config);

    genai_text_init_session(session);
    int a = 100;
    genai_text_result_set_callback(session, callback, &a);
    genai_text_chat_async(session, "一加一等于几");

    while (std::getchar() != '\n') {
    }

    genai_text_chat_async(session, "你说的不对");

    while (std::getchar() != '\n') {
    }
    genai_text_stop_chat(session);
    genai_text_destroy_session(&session);
    chat_model_config_destroy(&config);
    g_main_loop_quit(pMainLoop);
}

void chatHistoryMessages() {
    GMainLoop *pMainLoop = g_main_loop_new(nullptr, false);

    std::thread ctrlThread([pMainLoop] {
        g_main_loop_run(pMainLoop);
        g_main_loop_unref(pMainLoop);
    });
    ctrlThread.detach();

    ChatModelConfig *config = chat_model_config_create();
    chat_model_config_set_name(config, "百度-ERNIE-Bot-4");
    chat_model_config_set_top_k(config, 0.5);
    chat_model_config_set_deploy_type(config, ModelDeployType::PublicCloud);

    GenAiTextSession *session = genai_text_create_session();
    genai_text_set_model_config(session, config);

    genai_text_init_session(session);
    genai_text_result_set_callback(session, callback, nullptr);

    ChatMessage *chatMessage = chat_message_create();
    chat_message_add_system_message(chatMessage, "");
    chat_message_add_user_message(chatMessage, "一加一等于几");
    chat_message_add_system_message(chatMessage,
                                    "这是一个非常基础的数学问题，"
                                    "、涉及到的是加法运算。题目问"
                                    "的是1+1等于几。\n\n在数学中，"
                                    "加法是一种基本的运算方式，表"
                                    "示两个数量的和。当我们把两个1"
                                    "加在一起时，就是在计算这两个"
                                    "数量的总和。\n\n所以，1 + 1 ="
                                    " 2。\n\n因此，答案是2。这个问"
                                    "题非常直接，没有涉及到复杂的"
                                    "数学概念或技巧，只需要理解加"
                                    "法的基本定义即可。");
    chat_message_add_user_message(chatMessage, "你说的不对");

    genai_text_chat_with_history_messages_async(session, chatMessage);

    while (std::getchar() != '\n') {
    }
    chat_message_destroy(&chatMessage);
    genai_text_stop_chat(session);
    genai_text_destroy_session(&session);
    chat_model_config_destroy(&config);
    g_main_loop_quit(pMainLoop);
}

void clearChatMessage() {
    GMainLoop *pMainLoop = g_main_loop_new(nullptr, false);

    std::thread ctrlThread([pMainLoop] {
        g_main_loop_run(pMainLoop);
        g_main_loop_unref(pMainLoop);
    });
    ctrlThread.detach();

    ChatModelConfig *config = chat_model_config_create();
    chat_model_config_set_name(config, "百度-ERNIE-Bot-4");
    chat_model_config_set_top_k(config, 0.5);
    chat_model_config_set_deploy_type(config, ModelDeployType::PublicCloud);

    GenAiTextSession *session = genai_text_create_session();
    genai_text_set_model_config(session, config);

    genai_text_init_session(session);
    genai_text_result_set_callback(session, callback, nullptr);
    genai_text_chat_async(session, "一加一等于几");

    while (std::getchar() != '\n') {
    }

    genai_text_clear_chat_history_messages(session);

    while (std::getchar() != '\n') {
    }

    genai_text_chat_async(session, "你说的不对");

    while (std::getchar() != '\n') {
    }

    genai_text_stop_chat(session);
    genai_text_destroy_session(&session);
    chat_model_config_destroy(&config);
    g_main_loop_quit(pMainLoop);
}

void generateContent() {
    GMainLoop *pMainLoop = g_main_loop_new(nullptr, false);

    std::thread ctrlThread([pMainLoop] {
        g_main_loop_run(pMainLoop);
        g_main_loop_unref(pMainLoop);
    });
    ctrlThread.detach();

    ChatModelConfig *config = chat_model_config_create();
    chat_model_config_set_name(config, "百度-ERNIE-Bot-4");
    chat_model_config_set_top_k(config, 0.5);
    chat_model_config_set_deploy_type(config, ModelDeployType::PublicCloud);

    GenAiTextSession *session = genai_text_create_session();
    genai_text_set_model_config(session, config);

    genai_text_init_session(session);
    genai_text_result_set_callback(session, callback, nullptr);
    genai_text_generate_content_async(session, "今天天气不错");

    while (std::getchar() != '\n') {
    }
    genai_text_stop_chat(session);
    genai_text_destroy_session(&session);
    g_main_loop_quit(pMainLoop);
}


void chatSystemPromptId(PromptId promptId, const std::string &question) {
    GMainLoop *pMainLoop = g_main_loop_new(nullptr, false);

    std::thread ctrlThread([pMainLoop] {
        g_main_loop_run(pMainLoop);
        g_main_loop_unref(pMainLoop);
    });
    ctrlThread.detach();

    ChatModelConfig *config = chat_model_config_create();
    chat_model_config_set_name(config, "百度-ERNIE-Bot-4");
    chat_model_config_set_top_k(config, 0.5);
    chat_model_config_set_deploy_type(config, ModelDeployType::PublicCloud);

    GenAiTextSession *session = genai_text_create_session();
    genai_text_set_model_config(session, config);

    genai_text_init_session(session);
    genai_text_result_set_callback(session, callback, nullptr);
    genai_text_set_chat_system_prompt_id(session, promptId);
    genai_text_chat_async(session, question.c_str());

    while (std::getchar() != '\n') {
    }
    genai_text_stop_chat(session);
    genai_text_destroy_session(&session);
    g_main_loop_quit(pMainLoop);
}

void chatSystemPromptId_SUMMARY() {
    chatSystemPromptId(
        SUMMARY,
        "大模型是人工智能领域的热门研究方向。专家认为，人工智能进"
        "入产业级大模型时代。大模型将是未来一段时间科技领域里面最重要的事情之一"
        "。大模型将开启人工智能的“大一统时代”。");
}

void chatSystemPromptId_TEXT_EXPANSION() {
    chatSystemPromptId(TEXT_EXPANSION, "今天天气不错");
}

void chatSystemPromptId_TRANSLATE_CHINESE_TO_ENGLISH() {
    chatSystemPromptId(TRANSLATE_CHINESE_TO_ENGLISH,"今天天气不错");
}

int main(int argc, char *argv[])
{
    chatSystemPromptId_TRANSLATE_CHINESE_TO_ENGLISH();
    return 0;
}


